package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.szewec.ps.R;
import com.szewec.ps.util.StringUtils;

public class MultipleInputItem extends ConstraintLayout implements BaseInput {
    protected TextView itemName;
    protected TextView itemValue;
    private CharSequence hint;


    public MultipleInputItem(Context context) {
        super(context);
        init(context,null);
    }

    public MultipleInputItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public MultipleInputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.multiple_input_item_view, this);
        itemName = findViewById(R.id.tv_item_name);
        itemValue = findViewById(R.id.et_item_value);
        //记录原始hint
        hint = itemValue.getHint();
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItem);
            String name = ta.getString(R.styleable.InputItem_itemName);
            itemName.setText(name);

            boolean required = ta.getBoolean(R.styleable.InputItem_required,false);

            if (!required){
                itemName.setCompoundDrawables(null,null,null,null);
            }

            ta.recycle();
        }
    }

    public String getStringValue(){
        if(itemValue.getText() == null || StringUtils.isEmpty(itemValue.getText().toString())){
            return null;
        }else {
            String value = itemValue.getText().toString();
            return value;
        }
    }
    public Double getDoubleValue(){
        if(itemValue.getText() == null){
            return null;
        }else {
            Double value = Double.valueOf(itemValue.getText().toString());
            return value;
        }
    }
    public Integer getIntValue(){
        if(itemValue.getText() == null){
            return null;
        }else {
            Integer value = Integer.valueOf(itemValue.getText().toString());
            return value;
        }
    }

    public void setValue(Object value){
        if (value == null){
            return;
        }
        itemValue.setText(value.toString());
    }

    /**
     * 设置是否必填
     * @param context
     * @param required
     */
    public void setRequired(Context context,boolean required){
        if (required){
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.red_star);
            itemName.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
        }else {
            itemName.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
        }
    }

    /**
     * textChange事件
     * @param watcher
     */
    public void setTextChangeListener(TextWatcher watcher){
        if (watcher == null){
            return;
        }
        itemValue.addTextChangedListener(watcher);
    }

    /**
     * 切换textview
     *
     */
    @Override
    public void switchoverMode(boolean isEnabled) {
        if (isEnabled) {
            itemValue.setHint(hint);
            itemValue.setCompoundDrawablesWithIntrinsicBounds(0,0, R.drawable.flag_right, 0);
        } else {
            itemValue.setHint("");
            itemValue.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
        }
    }

    /**
     * 设置title名称
     * @param itemName
     */
    public void setItemName(String itemName){
        this.itemName.setText(itemName);
    }
}
